package com.qd.p2p.web.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.ScoreLotteryRecordService;
import com.qdlc.p2p.biz.service.lottery.LotteryService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.biz.zdata.LotteryResult;
import com.qdlc.p2p.common.constant.LotteryCategory;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.dal.model.ScoreLotteryRecordModel;

/**
 * 积分抽奖
 *
 * @author xxx
 * @version 2.0
 * @date 2015年6月23日 下午3:16:27
 */
public class ScoreLotteryAction extends BaseAction<ScoreLotteryRecordModel> implements ModelDriven<ScoreLotteryRecordModel> {

    @Resource
    private ScoreLotteryRecordService scoreLotteryRecordService;

    /** 积分抽奖服务 */
    @Resource
    @Qualifier("scoreDrawLotteryService")
    private LotteryService scoreDrawLotteryService;

    private Map<String, Object> data;

    /**
     * 积分抽奖页面
     *
     * @return
     */
    @Action(value = "/member/score/mall/lottery")
    public String lottery() {
        return "lottery";
    }

    /**
     * 积分抽奖记录列表
     *
     * @return
     * @throws IOException
     */
    @Action(value = "/member/score/mall/lottery/list", interceptorRefs = {@InterceptorRef("ajaxSafe"), @InterceptorRef("globalStack")})
    public void list() throws IOException {
        List<ScoreLotteryRecordModel> list = scoreLotteryRecordService.getNewList();
        data = new HashMap<String, Object>();
        data.put("data", list);
        printWebJson(getStringOfJpaObj(data));
    }

    /**
     * 积分抽奖
     *
     * @return
     * @throws IOException
     */
    @Action("/member/score/mall/lottery/doLottery")
    public void doLottery() throws IOException {
        if (getSessionUser() == null) {
            throw new UserException("请登录", BusinessException.TYPE_JSON);
        }
        model.setUser(getSessionUser());
        data = new HashMap<String, Object>();

        // 积分抽奖
        LotteryResult lottery = (LotteryResult) scoreDrawLotteryService.process(model.getUser(), LotteryCategory.SCORE.getCategory());

        // 转盘对应角度设置
        Random random = new Random();
        switch (lottery.getPos())
        {
            case 1:
                lottery.setPos(0 + random.nextInt(51 - 0));
                break;
            case 2:
                lottery.setPos(51 + random.nextInt(107 - 51));
                break;
            case 3:
                lottery.setPos(154 + random.nextInt(203 - 154));
                break;
            case 4:
                lottery.setPos(204 + random.nextInt(254 - 204));
                break;
            case 5:
                lottery.setPos(254 + random.nextInt(305 - 254));
                break;
            case 6:
                lottery.setPos(107 + random.nextInt(154 - 107));
                break;
            case 7:
                lottery.setPos(305 + random.nextInt(359 - 305));
                break;
        }
        data.put("result", lottery);
        printWebJson(getStringOfJpaObj(data));
    }
}
